package com.magic.himalaya.adapters;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.magic.himalaya.R;
import com.magic.himalaya.utils.LogUtil;
import com.squareup.picasso.Picasso;
import com.ximalaya.ting.android.opensdk.model.album.Album;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.List;

/**
 * @description:
 * @create: Create by Magic on 2022/1/20.
 */
public class RecommendListAdapter extends RecyclerView.Adapter<RecommendListAdapter.ItemViewHolder> {
    private static final String TAG = RecommendListAdapter.class.getSimpleName();

    private List<Album> albumList = new ArrayList<>();

    public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //载入VIew
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_recommend_recyclerview, parent, false);

        return new ItemViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull ItemViewHolder holder, int position) {
        //封装数据,设置数据
        holder.itemView.setTag(position);
        holder.setData(albumList.get(position));

        //设置点击事件
        holder.itemView.setOnClickListener(v -> {
            if (onCallBack != null)
                onCallBack.onClickToDetailActivity(albumList.get(position),position);
        });
    }

    @Override
    public int getItemCount() {
        //显示需要返回的个数
        if (albumList != null)
            return albumList.size();
        return 0;
    }

    public void setData(List<Album> albumList) {
        if (albumList != null) {
            this.albumList.clear();
            this.albumList.addAll(albumList);
        }
        //更新一下UI
        notifyDataSetChanged();
    }

    class ItemViewHolder extends RecyclerView.ViewHolder {
        public ItemViewHolder(@NonNull View itemView) {
            super(itemView);
        }

        public void setData(Album album) {

            //找到各个控件，设置数据
            ImageView image_small = itemView.findViewById(R.id.cover_url_small);//专辑图片
            TextView text_title = itemView.findViewById(R.id.albumTitle);//专辑标题
            TextView text_intro = itemView.findViewById(R.id.album_intro);//专辑简介
            TextView text_playCount = itemView.findViewById(R.id.playCount);//专辑播放量
            TextView text_includeTrackCount = itemView.findViewById(R.id.includeTrackCount);//专辑总集数量


            if (!album.getCoverUrlSmall().equals("") || album.getCoverUrlSmall().length() > 0) {
                Picasso.get().load(album.getCoverUrlSmall()).into(image_small);
            }
            text_title.setText(album.getAlbumTitle());
            text_intro.setText(album.getAlbumIntro());
            text_playCount.setText(album.getPlayCount() + "播放");
            text_includeTrackCount.setText(" " + album.getIncludeTrackCount() + "集");

        }
    }


    private OnCallBack onCallBack = null;

    public void setOnCallBack(OnCallBack onCallBack) {
        this.onCallBack = onCallBack;
    }

    public interface OnCallBack {
        void onClickToDetailActivity(Album album,int position);
    }
}
